package com.bitbank.smartdevice.adapter.base

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.support.v7.widget.RecyclerView
import android.text.util.Linkify
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.widget.*
import com.bitbank.smartdevice.util.GlideApp

/**
 *  Date：2018/12/10
 *  Author：xulee
 *  Desc：
 */
class MViewHolder(private val convertView: View) : RecyclerView.ViewHolder(convertView) {

    private val mViews: SparseArray<View> = SparseArray()

    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @return
     */
    fun getView(viewId: Int): View {
        var view = mViews.get(viewId)
        if (view == null) {
            view = convertView.findViewById(viewId)
            mViews.put(viewId, view)
        }
        return view
    }

    fun getConvertView(): View = convertView
    /****以下为辅助方法 */

    /**
     * 设置TextView的值
     *
     * @param viewId
     * @param text
     * @return
     */
    fun setText(viewId: Int, text: String): MViewHolder {
        val tv = getView(viewId) as TextView
        tv.text = text
        return this
    }

    fun setImageResource(viewId: Int, resId: Int): MViewHolder {
        val view = getView(viewId) as ImageView
        view.setImageResource(resId)
        return this
    }

    fun setImageBitmap(viewId: Int, bitmap: Bitmap): MViewHolder {
        val view = getView(viewId) as ImageView
        view.setImageBitmap(bitmap)
        return this
    }

    fun setImageDrawable(viewId: Int, drawable: Drawable): MViewHolder {
        val view = getView(viewId) as ImageView
        view.setImageDrawable(drawable)
        return this
    }

    fun setImageUrl(viewId: Int, imageUrl: String): MViewHolder {
        val view = getView(viewId) as ImageView
        GlideApp.with(view.context)
            .load(imageUrl)
            .into(view)
        return this
    }

    fun setBackgroundColor(viewId: Int, color: Int): MViewHolder {
        val view = getView(viewId)
        view.setBackgroundColor(color)
        return this
    }

    fun setBackgroundRes(viewId: Int, backgroundRes: Int): MViewHolder {
        val view = getView(viewId)
        view.setBackgroundResource(backgroundRes)
        return this
    }

    fun setTextColor(viewId: Int, textColor: Int): MViewHolder {
        val view = getView(viewId) as TextView
        view.setTextColor(textColor)
        return this
    }

    fun setTextColorRes(viewId: Int, textColorRes: Int): MViewHolder {
        val view = getView(viewId) as TextView
        view.setTextColor(view.resources.getColor(textColorRes))
        return this
    }

    @SuppressLint("NewApi")
    fun setAlpha(viewId: Int, value: Float): MViewHolder {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getView(viewId).setAlpha(value)
        } else {
            // Pre-honeycomb hack to set Alpha value
            val alpha = AlphaAnimation(value, value)
            alpha.duration = 0
            alpha.fillAfter = true
            getView(viewId).startAnimation(alpha)
        }
        return this
    }

    fun setVisible(viewId: Int, visible: Boolean): MViewHolder {
        val view = getView(viewId)
        view.visibility = if (visible) View.VISIBLE else View.GONE
        return this
    }

    fun linkify(viewId: Int): MViewHolder {
        val view = getView(viewId) as TextView
        Linkify.addLinks(view, Linkify.ALL)
        return this
    }

    fun setTypeface(typeface: Typeface, vararg viewIds: Int): MViewHolder {
        for (viewId in viewIds) {
            val view = getView(viewId) as TextView
            view.typeface = typeface
            view.paintFlags = view.paintFlags or Paint.SUBPIXEL_TEXT_FLAG
        }
        return this
    }

    fun setProgress(viewId: Int, progress: Int): MViewHolder {
        val view = getView(viewId) as ProgressBar
        view.progress = progress
        return this
    }

    fun setProgress(viewId: Int, progress: Int, max: Int): MViewHolder {
        val view = getView(viewId) as ProgressBar
        view.max = max
        view.progress = progress
        return this
    }

    fun setMax(viewId: Int, max: Int): MViewHolder {
        val view = getView(viewId) as ProgressBar
        view.max = max
        return this
    }

    fun setRating(viewId: Int, rating: Float): MViewHolder {
        val view = getView(viewId) as RatingBar
        view.rating = rating
        return this
    }

    fun setRating(viewId: Int, rating: Float, max: Int): MViewHolder {
        val view = getView(viewId) as RatingBar
        view.max = max
        view.rating = rating
        return this
    }

    fun setTag(viewId: Int, tag: Any): MViewHolder {
        val view = getView(viewId)
        view.setTag(tag)
        return this
    }

    fun setTag(viewId: Int, key: Int, tag: Any): MViewHolder {
        val view = getView(viewId)
        view.setTag(key, tag)
        return this
    }

    fun setChecked(viewId: Int, checked: Boolean): MViewHolder {
        val view = getView(viewId) as Checkable
        view.isChecked = checked
        return this
    }

    /**
     * 关于事件的
     */
    fun setOnClickListener(
        viewId: Int,
        listener: View.OnClickListener
    ): MViewHolder {
        val view = getView(viewId)
        view.setOnClickListener(listener)
        return this
    }

    fun setOnTouchListener(
        viewId: Int,
        listener: View.OnTouchListener
    ): MViewHolder {
        val view = getView(viewId)
        view.setOnTouchListener(listener)
        return this
    }

    fun setOnLongClickListener(
        viewId: Int,
        listener: View.OnLongClickListener
    ): MViewHolder {
        val view = getView(viewId)
        view.setOnLongClickListener(listener)
        return this
    }

    companion object {

        fun createViewHolder(context: Context, itemView: View): MViewHolder {
            return MViewHolder(itemView)
        }

        fun createViewHolder(
            context: Context,
            parent: ViewGroup, layoutId: Int
        ): MViewHolder {
            val itemView = LayoutInflater.from(context).inflate(
                layoutId, parent,
                false
            )
            return MViewHolder(itemView)
        }
    }

}